<?php

namespace App\Model\form;

use App\Constants\ConstCode;
use App\Model\UserDevice;
use Hyperf\Database\Model\Builder;
use Hyperf\Database\Model\Model;

trait UserDeviceTrait
{
    /**
     * 根据登录unionId查询设备
     * @param string $unionId
     * @return Builder|Model|object|null
     */
    public function getUserDeviceInfo(string $unionId)
    {
        return UserDevice::where(['unionId' => $unionId, 'status' => ConstCode::IS_USE])->first();
    }

    /**
     * 验证token是否存在
     * @param string $token
     * @return Builder|Model|object|null
     */
    public function getTokenInfo(string $token)
    {
        return UserDevice::where(['token' => $token, 'status' => ConstCode::IS_USE])->first();
    }

    /**
     * 根据条件获取设备id
     * @param $where
     * @return int
     */
    public function getUserDeviceId($where): int
    {
        return UserDevice::where($where)->value('id');
    }

    /**
     * 根据条件获取设备token
     * @param $where
     * @return string
     */
    public function getUserDeviceToken($where): string
    {
        return UserDevice::where($where)->value('token');
    }


}